#!/usr/bin/env python
# -*- coding: utf-8 -*-


from utils import util
from config import conf

"""url_like collection has
   urlid:       (int)
   uuid:        (str)
   atime:       (str)
"""
"""topic_like collection has
   topicid:     (int)
   uuid:        (str)
   atime:       (str)
"""


def find_url_like(urlid):
    like_datas = conf.db.url_like.find({
        'urlid': long(urlid),
    }).sort('_id', 1)
    return [like_data['uuid'] for like_data in like_datas]


def get_url_like(urlid, uuid):
    like_datas = conf.db.url_like.find({
        'urlid': long(urlid),
    }).sort('_id', 1)
    like = {'is_liked': False, 'likes': []}
    likes = []
    for like_data in like_datas:
        if like_data['uuid'] == uuid:
            like['is_liked'] = True
        likes.append({
            'urlid': like_data['urlid'],
            'uuid': like_data['uuid'],
            'atime': str(like_data['_id'].generation_time).split(' ')[0],
        })
    like['likes'] = likes
    return like


def add_url_like(urlid, uuid):
    new_like = {
        'urlid': long(urlid),
        'uuid': uuid,
        'atime': util.datetime_now_str(),
    }
    op = conf.db.url_like.insert(new_like)
    if not op:
        return None
    return conf.db.url_like.find({
        'urlid': long(urlid),
    }).count()


def del_url_like(urlid, uuid):
    op = conf.db.url_like.remove(
        {
            'urlid': long(urlid),
            'uuid': uuid,
        },
    )
    if not op:
        return None
    return conf.db.url_like.find({
        'urlid': long(urlid),
    }).count()


def del_url_like_by_code(urlid):
    return conf.db.url_like.remove(
        {
            'urlid': long(urlid),
        },
    )


def find_topic_like(topicid):
    like_datas = conf.db.topic_like.find({
        'topicid': long(topicid),
    }).sort('_id', 1)
    return [like_data['uuid'] for like_data in like_datas]


def get_topic_like(topicid, uuid):
    like_datas = conf.db.topic_like.find({
        'topicid': long(topicid),
    }).sort('_id', 1)
    like = {'is_liked': False, 'likes': []}
    likes = []
    for like_data in like_datas:
        if like_data['uuid'] == uuid:
            like['is_liked'] = True
        likes.append({
            'topicid': like_data['topicid'],
            'uuid': like_data['uuid'],
            'atime': str(like_data['_id'].generation_time).split(' ')[0],
        })
    like['likes'] = likes
    return like


def add_topic_like(topicid, uuid):
    new_like = {
        'topicid': long(topicid),
        'uuid': uuid,
        'atime': util.datetime_now_str(),
    }
    op = conf.db.topic_like.insert(new_like)
    if not op:
        return None
    return conf.db.topic_like.find({
        'topicid': long(topicid),
    }).count()


def del_topic_like(topicid, uuid):
    op = conf.db.topic_like.remove(
        {
            'topicid': long(topicid),
            'uuid': uuid,
        },
    )
    if not op:
        return None
    return conf.db.topic_like.find({
        'topicid': long(topicid),
    }).count()
